home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / utilities / arexx / events.rexx next >
OS/2 REXX Batch file  |  1995-03-17  |  10KB  |  300 lines

  1. /***************************************************************************/
  2. /* Events.rexx                                                             */
  3. /* A reminder for events through the year                                  */
  4. /* Cedric BEUST (beust@taloa.unice.fr), Oct 91                             */
  5. /* This program is in public domain                                        */
  6. /*                                                                         */
  7. /* The entries in the file 's:.eventsconfig' look like this:               */
  8. /*    01 nov 91,5,       Foo's birthday                                    */
  9. /*    --------- - -----------------------------------------                */
  10. /*        |     |                   |                                      */
  11. /*        |     |                   +-> A comment that will be displayed   */
  12. /*        |     +---------> Warn how many days BEFORE the event            */
  13. /*        +--------> The event                                             */
  14. /*                                                                         */
  15. /* A valid entry is one that starts with a numeric. Any other is a comment */
  16. /* The menus are self-explanatory...                                       */
  17. /* This program is meant to be inserted in your startup-sequence (or       */
  18. /* you user-startup). It is not very robust but this can be easily fixed   */
  19. /* (I started to write it yesterday...)                                    */
  20. /* Before doing any operation on the configfile, it is copied to           */
  21. /* 's:.eventsconfig.old'                                                   */
  22. /*                                                                         */
  23. /* Syntax:      events [-n]                                                */
  24. /*                  -n:     Display next event to come                     */
  25. /*                                                                         */
  26. /***************************************************************************/
  27.  
  28. arg options
  29. closesteventdays = 10000
  30.  
  31.    /* Files used */
  32. configfile = "s:.eventsconfig"
  33. tmpfilefrom = 't:event.tmp'
  34. tmpfileto = 't:event.new'
  35.  
  36.    /* Copy the config file for lock considerations */
  37. address command
  38. "copy " configfile tmpfileto
  39.  
  40.    /* Try to read the configuration file */
  41. if ~open(file, configfile, 'Read') then do
  42.    say "Can't open configuration file " configfile ". Exiting..."
  43.    exit 10
  44. end
  45.  
  46.    /* Calculate how many days there has been for current date */
  47. currentdays = convertToDays(date('Normal'))
  48.  
  49.    /* Check out each event in the config file */
  50. do until eof(file)
  51.    line = readln(file)
  52.    parse value line with date "," delay "," comment
  53.    parse var date dd mmm yy
  54.    if datatype(dd, 'Numeric') then
  55.    do
  56.       eventdays = convertToDays(date)
  57.       if index(options, '-N') then
  58. /* We only display the closest event, so we check if this one is closer than */
  59. /* the previous one */
  60.       do
  61.          if (eventdays - currentdays < closesteventdays) then
  62.          do
  63.             closesteventdays = eventdays - currentdays
  64.             closestevent = line
  65.          end
  66.       end
  67.       else if (eventdays - currentdays <= delay & eventdays - currentdays > 0) then
  68.       do
  69.  
  70. /* This event is due, display an appropriate message */
  71.          call displayEvent(line)
  72.       end
  73.    end
  74. end
  75.  
  76. if index(options, '-N') then
  77. do
  78.    parse var closestevent date "," delay "," comment
  79.    howmanyleft = convertToDays(date) - convertToDays(date('Normal'))
  80.    say "Next event: <" || strip(comment) || "> on " || date || " (in " || howmanyleft || " days)"
  81.    say "You will be warned " || delay || " days before"
  82. end
  83. call close(file)
  84.  
  85.    /* Update the configuration file */
  86. "copy " tmpfileto configfile
  87. return 0
  88.  
  89. /***************************************************************************/
  90.  
  91. displayEvent:
  92. /* An event is due, display its comment in a window */
  93.    arg line
  94.  
  95. parse var line date "," delay "," comment
  96.  
  97. windev = "CON:1/1/639/199/ ** EVENT **"
  98.  
  99. if ~open('console',windev) then
  100. do
  101.    say "Couldn't open" windev
  102.    exit      /* Window for all activity */
  103. end
  104.  
  105.  
  106.    /* Put some colors in the menu... */
  107. nexttimekey = 'T'
  108. nextmonthkey = 'M'
  109. nextyearkey = 'Y'
  110. ndayskey = 'N'
  111. deletekey = 'D'
  112.  
  113. nexttimecode = '1B'x || '[33m' || nexttimekey || '1B'x || '[32m'
  114. nextmonthcode = '1B'x || '[33m' || nextmonthkey || '1B'x || '[32m'
  115. nextyearcode = '1B'x || '[33m' || nextyearkey || '1B'x || '[32m'
  116. ndayscode = '1B'x || '[33m' || ndayskey || '1B'x || '[32m'
  117. deletecode = '1B'x || '[33m' || deletekey || '1B'x || '[32m'
  118.  
  119. howmanyleft = convertToDays(date) - convertToDays(date('Normal'))
  120. call writeln('console', "")
  121. call writeln('console', 'In' howmanyleft 'days' || '1B'x || '[33m' || ' (' || date || ')')
  122. call writeln('console', "")
  123. call writeln('console', '1B'x || '[31m' || (center(comment,75)))
  124. call writeln('console', '1B'x || '[32m')
  125. call writeln('console', "")
  126. call writeln('console', center("Now what?",75))
  127. call writeln('console', "     Remind next " || nexttimecode || ")ime")
  128. call writeln('console', "     Remind again next " || nextmonthcode || ")onth")
  129. call writeln('console', "     Remind again next " || nextyearcode || ")ear")
  130. call writeln('console', "     Remind again in " || ndayscode || ") days")
  131. call writeln('console', "     " || deletecode || ")elete this entry")
  132. call writeln('console', "")
  133. call writech('console',"      -> ")
  134.  
  135. valid = 0
  136. do while valid = 0
  137. do
  138.    answer = readln('console')
  139.    select
  140.       when answer = 't' then do valid = 1; end
  141.       when answer = 'm' then do forgetEntry('m',line); valid = 1; end
  142.       when answer = 'y' then do forgetEntry('y',line); valid = 1; end
  143.       when answer = 'd' then do deleteEntry(line); valid = 1; end
  144.       when answer = 'n' then do remindNDays(line); valid = 1; end
  145.       otherwise valid = 1           /* default: remind next time */
  146.    end
  147.    
  148. end
  149. call close('console')
  150. return 0
  151.  
  152. /***************************************************************************/
  153. forgetEntry:
  154. /* Forget entry till next year, month or time */
  155. /* 'whenagain' is a letter saying what part must be increased */
  156.  
  157.    arg whenagain, line
  158.  
  159.    parse value line with date "," delay "," comment
  160.  
  161.    months.1 = 'Jan'; months.2 = 'Feb'; months.3 = 'Mar'; months.4 = 'Apr'
  162.    months.5 = 'May'; months.6 = 'Jun'; months.7 = 'Jul'; months.8 = 'Aug'
  163.    months.9 = 'Sep'; months.10 = 'Oct'; months.11 = 'Nov'; months.12 = 'Dec'
  164.    months.13 = 'Jan'
  165.  
  166.    months.jan = 1; months.feb = 2; months.mar = 3; months.apr = 4
  167.    months.may = 5; months.jun = 6; months.jul = 7; months.aug = 8
  168.    months.sep = 9; months.oct = 10; months.nov = 11 ; months.dec = 12
  169.  
  170.    parse var date dd mmm yy
  171.    select
  172.       when whenagain = 'Y' then
  173.       do
  174.          yy = yy + 1
  175.          modifiedline = dd || " " || mmm || " " || yy || ","
  176.          modifiedline = modifiedline || delay || "," || comment
  177.       end
  178.       when whenagain = 'M' then
  179.       do
  180.          mm = months.mmm + 1
  181.          newmonth = months.mm
  182.          if newmonth = 'Jan' then
  183.          do
  184.             yy = yy + 1
  185.             newmonth = 'Jan '
  186.          end
  187.          modifiedline = dd || " " || newmonth || yy || ","
  188.          modifiedline = modifiedline || delay || "," || comment
  189.       end
  190.       otherwise
  191.       do
  192.          say "Unexpected value " whenagain
  193.          whenagain = 'T'
  194.       end
  195.    end
  196.    if whenagain ~= 'T' then    /* we don't do anything if it's T */ 
  197.       call modifyLine(line, modifiedline)
  198.  
  199.    return 0
  200.  
  201. /***************************************************************************/
  202. deleteEntry:
  203. /* Delete this entry from the config file          */
  204. /* Save the old file in 's:.eventsconfig.old'      */
  205.  
  206.    arg line
  207.  
  208.    call modifyLine(line,"")
  209.    return 0
  210.  
  211. /***************************************************************************/
  212. remindNDays:
  213. /* Ask the user in how many days they want to be reminded again */
  214.  
  215.    arg line
  216.  
  217.    parse value line with date "," delay "," comment
  218.    answer = -1
  219.  
  220.    howmanyleft = convertToDays(date) - convertToDays(date('Normal'))
  221.  
  222.    call writeln('console','')
  223.    do while answer < 0 | answer > delay
  224.       call writech('console', "In how many days shall I remind you again ? (0-" || howmanyleft || ") ")
  225.  
  226.       answer = readln('console')
  227.    end
  228.    
  229.    modifiedline = date || "," || (howmanyleft - answer) || "," || comment
  230.    call modifyLine(line, modifiedline)
  231.  
  232.    return 0
  233.  
  234. /***************************************************************************/
  235. modifyLine:
  236. /* Replace in the configline the oldline by the newline */
  237.    arg oldline, newline
  238.  
  239.    parse value oldline with date "," delay "," comment
  240.  
  241.    address command
  242.    "copy " configfile configfile'.old'
  243.    "copy " configfile tmpfilefrom
  244.  
  245.    /* Open both files. No checking, they *ought* to be there! */
  246.    s = open(to, tmpfileto, 'Write')
  247.    s = open(from, tmpfilefrom, 'Read')
  248.  
  249.    linein = readln(from)
  250.    do while ~eof(from)
  251.       if index(upper(linein), upper(comment)) = 0 then
  252.       do
  253.          call writeln(to, linein)
  254.       end
  255.       else
  256.       do
  257.          call writeln(to, newline)
  258.       end
  259.       linein = readln(from)
  260.    end
  261.    call close(to)
  262.    call close(from)
  263.    return 0
  264.  
  265. /***************************************************************************/
  266. convertToDays: procedure
  267. /* Date is 'dd mmm yy'. Return number of days since time 0 */
  268.    arg date
  269.  
  270.    parse var date dd mmm year
  271.  
  272. /* Set up the months table - from names to numbers, */
  273.    months. = 0; months.jan = 1; months.feb = 2; months.mar = 3
  274.    months.apr = 4; months.may = 5; months.jun = 6; months.jul = 7
  275.    months.aug = 8; months.sep = 9; months.oct = 10; months.nov = 11
  276.    months.dec = 12
  277.  
  278. /* and now from numbers to days/month & print names */
  279.    months.1.days = 31; months.2.days = 1   /* Fixed later */
  280.    months.3.days = 31; months.4.days = 30; months.5.days = 31
  281.    months.6.days = 30; months.7.days = 31; months.8.days = 31
  282.    months.9.days = 30; months.10.days = 31; months.11.days = 30
  283.    months.12.days = 31      /* Not needed, but here for completeness */
  284.  
  285.    if (year < 100) then yy = 1900 + year
  286.    else yy = year
  287.    if (yy // 4 = 0 & yy // 100 ~= 0) then months.2.days = 29  /* a leap year */
  288.    else months.2.days = 28
  289.    mmm = upper(left(mmm,3))
  290.    themonth = months.mmm
  291.  
  292. /* summonth is the number of days in the previous months */
  293.    summonth = 0
  294.    do i = 1 to themonth - 1
  295.       summonth = summonth + months.i.days
  296.    end
  297.  
  298.    return (yy - 1) * 365 + summonth +dd
  299.  
  300.